//
//  NiceCell.swift
//  MD5
//
//  Created by 郑东喜 on 2016/11/24.
//  Copyright © 2016年 郑东喜. All rights reserved.
//

import UIKit

class NiceCell: UIViewController {
    
    
    /**
     
     * 九宫格的创建是有规律可循的,每一个格子的水平和竖直方向的间隔是固定的
     * ,通过计算每一个格子的位置,然后把它们依次放到视图中即可。
     *
     * 一般的步骤为:
     * 1.设置格子的总个数和每一列的格子数
     * 2.求格子的间隔:
     *   水平间隔 = 视图总宽-每一个格子的宽*列数)/(列数+1)
     *   竖直间隔 = 水平间隔
     * 3.通过for循环,依次创建所有的格子并设置相应的图片,并且对每一个格子的位置进行设置
     */
    //表格数据源
    lazy var dataArr : NSMutableArray = {
        var data = NSMutableArray()
        data = ["1000","-200","10","49","-40"]
        return data
    }()
    
    // 声明属性
    var button :UIButton?
    var image :UIImage?
    var txt : String?
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        
        // 设置格子的高和宽
        self.image = UIImage(named: "0.jpg")
        
        view.backgroundColor = UIColor.white
        
        let heigth:CGFloat = 30
        let width:CGFloat =  (UIScreen.main.bounds.width - 40) / 3
  
        
        let col = 3
        
        
        // 设置格子的间距
        let screenSize:CGSize = self.view.frame.size
        
        let hMargin:CGFloat = (screenSize.width - (CGFloat(col) * width)) / CGFloat((col+1))
        let vMargin:CGFloat = hMargin
        
        var row:Int = 0
        for i in 0..<dataArr.count {
            self.button = UIButton()
//            self.image! = UIImage(named: "\(i).jpg")!
//            self.button!.setImage(self.image!, for: .normal)
            
            self.button!.setTitle(dataArr[i] as? String, for: .normal)
            
            self.button?.setTitleColor(UIColor.black, for: .normal)
            
            self.button?.layer.cornerRadius = 3
            self.button?.layer.borderWidth = 1
            
//            self.button?.addTarget(self, action: "", for: .touchUpInside)
            self.button?.addTarget(self, action: #selector(NiceCell.touch(sender:)), for: .touchUpInside)

            if i%col == 0 {
                row += 1
            }
            
            let x:CGFloat = hMargin + (width + hMargin) * CGFloat(i%col)
            let y:CGFloat = vMargin + (heigth + vMargin) * CGFloat(row)
            

            self.button?.frame = CGRect(x: x, y: y, width: width, height: heigth)
            self.view.addSubview(self.button!)

        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}


extension NiceCell {
    func touch(sender:UIButton) -> Void {
        print("单机")
        print(sender.titleLabel?.text as Any)
        
        if sender.isSelected {
//            self.seleBtnImg.isHidden = true
            sender.backgroundColor = UIColor.red
//            localSave.set(sender.isSelected, forKey: agreeLaw)
        } else {
            sender.backgroundColor = UIColor.white
//            self.seleBtnImg.isHidden = false
//            localSave.set(sender.isSelected, forKey: agreeLaw)
        }
    }
}
